package com.tao.common.utils;


import com.tao.common.exception.BlogException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Slf4j
public class RedisUtils {

    private StringRedisTemplate redisTemplate;
    private static final String REDIS_SPLIT = ":";
    private static final String REDIS_DEFAULT_PREFIX = "default";



    public RedisUtils(StringRedisTemplate redisTemplate) {
//        log.info("RedisUtils: StringRedisTemplate初始化");
        this.redisTemplate = redisTemplate;
    }

    /**
     * @param key 获取 value
     * @return 返回对应的value, null 代表没有/不存在
     */
    public String get(String key) {
        try {
            return key == null ? null : redisTemplate.opsForValue().get(key);
        }catch (Exception e) {
            log.error("redis get error, key: {}, error: {}", key, e);
            return null;
        }
    }

    /**
     * @param key 判断 是否存在
     * @return true 存在 false 不存在
     */
    public boolean hasKey(String key) {
        if(key == null || key.length() == 0) {
            return false;
        }
        try {
            return redisTemplate.hasKey(key);
        }catch (Exception e) {
            log.error("redis hasKey error, key: {}, error: {}", key, e);
            return false;
        }
    }

    /**
     * 插入/修改 key value
     * @param key
     * @param value
     * @return true 成功, false 失败
     */
    public boolean set(String key, String value) {
        // 参数校验
        if(key == null || value == null || key.length() == 0 || value.length() == 0) {
            return false;
        }

        try {
            redisTemplate.opsForValue().set(key,value);
            return true;
        }catch (Exception e) {
            log.error("redis set error, key:{}, value:{}, error:{}", key,value,e);
            return false;
        }
    }
    public boolean set(String key, String value, Long time) {
        // 参数校验
        if(key == null || value == null || key.length() == 0 || value.length() == 0) {
            return false;
        }

        try {
            if(time >= 0){
                redisTemplate.opsForValue().set(key,value, time, TimeUnit.SECONDS);
            }else {
                redisTemplate.opsForValue().set(key,value);
            }
            return true;
        }catch (Exception e) {
            log.error("redis set error, key:{}, value:{}, time:{},  error:{}", key,value, time,e);
            return false;
        }
    }

    public String buildKey(String prefix, String... args) {
        // 参数校验
        if(!StringUtils.hasLength(prefix)) {
            prefix = REDIS_DEFAULT_PREFIX;
        }
        // 构造 Redis key
        StringBuffer key = new StringBuffer();
        key.append(prefix); // 添加前缀
        // 拼接后缀
        for(String arg : args) {
            key.append(REDIS_SPLIT);
            key.append(arg);
        }
        return key.toString();
    }

}
